<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">评论类型管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>评论类型管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="user-search-key">
                <option value="">-请选择-</option>
                <option value="type_name">类型名</option>
<!--                <option value="c_reply">回复量</option>-->
            </select>&emsp;
            <input id="user-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="user-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="user-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加类型</button>
            <button id="user-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
        </div>
        <!--数据表格-->
        <table class="layui-table" id="task-type-table" lay-filter="task-type-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="type-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
</script>

<!--&lt;!&ndash; 表格状态列 &ndash;&gt;-->
<!--<script type="text/html" id="user-tpl-state">-->
<!--    <input type="checkbox" lay-filter="user-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|已撤销"-->
<!--           {{d.tkflag==0?'checked':''}}/>-->
<!--</script>-->

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        let form = layui.form;
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let util = layui.util;
        let admin = layui.admin;

        let url = config.base_server + 'api-task/taskType/getTaskType';
        let params = {
            access_token: config.getToken().access_token
        };
        let cols = [[
            {type: 'numbers'},
            {field: 'typeName', sort: true, title: '任务类型名', align: 'center'},
            {field: 'description', sort: true, title: '描述', align: 'center'},
            {field: 'status', sort: true, title: '状态', align: 'center',templet:function (res) {
                    if (res.status==1)
                        return '正常';
                    else
                        return '冻结';
                }},
            {
                field: 'createTime', sort: true, align: 'center', templet: function (d) {
                    return util.toDateString(d.createTime);
                }, title: '创建时间'
            },
            {
                field: 'createTime', sort: true, align: 'center', templet: function (d) {
                    return util.toDateString(d.updateTime);
                }, title: '修改时间'
            },
            {templet: '#type-state', width: 120, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#task-type-table',
            url: url,
            method: 'POST',
            where: params,
            page: true,
            cols: cols
        })

        //添加按钮点击事件
        $('#user-btn-add').click(function () {
            showReplyModel();
        });

        // 工具条点击事件
        table.on('tool(task-type-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'edit') {
                showReplyModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('此操作不可逆，确定删除吗？', function () {
                    layer.load(2);
                    admin.req('api-task/taskType/' + obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('task-type-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');

                });
            }
        });

       // 显示表单弹窗
        let showReplyModel = function (data) {
            // //修改sex值
            // if (data){
            //     data.sex = data.sex+'';
            // }
            admin.putTempData('taskType', data);
            let title = data ? '编辑类型' : '添加类型';
            admin.popupCenter({
                title: title,
                path: 'pages/task/taskType_form.html',
                finish: function () {
                    table.reload('task-type-table', {});
                }
            });
        };
        // 条件检索
        form.on('submit(query)', (data) => {
            table.reload('task-type-table', {where: data.field});
            return false;
        });


        // 回车查询
        $('.enterSubmit').keyup((e) => {
            switch (e.keyCode) {
                case 13:
                    $('#user-btn-search').click();
                    break;
            }
        });
        //搜索按钮点击事件
        $('#user-btn-search').click(function () {
            let key = $('#user-search-key').val();
            let value = $('#user-search-value').val();
            table.reload('task-type-table', {where: {searchKey: key, searchValue: value}});
        });



        // 导出
        // $('#user-btn-export').click(function () {
        //     var url = config.base_server + 'api-user/users/exportUser?access_token='+config.getToken().access_token;
        //     var xhr = new XMLHttpRequest();
        //     xhr.open('POST', url, true);
        //     xhr.responseType = "blob";
        //     xhr.setRequestHeader("client_type", "DESKTOP_WEB");
        //     xhr.onload = function() {
        //         if (this.status == 200) {
        //             var blob = this.response;
        //             var objecturl = URL.createObjectURL(blob);
        //             window.location.href = objecturl;
        //         }
        //     }
        //     xhr.send();
        // });
    });
</script>